/*
 * Created by JFormDesigner on Sun Dec 25 21:54:09 ICT 2011
 */

package View;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.*;

import Dao.SearchDaoHelper;
import Model.SearchModel;

/**
 * @author Khainx
 */
public class SearchView extends JPanel {
	private SearchDaoHelper dao = new SearchDaoHelper();

	public SearchView() {
		initComponents();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		label1 = new JLabel();
		panel1 = new JPanel();
		label13 = new JLabel();
		label2 = new JLabel();
		txtID = new JTextField();
		label14 = new JLabel();
		label3 = new JLabel();
		txtName = new JTextField();
		label4 = new JLabel();
		label5 = new JLabel();
		cbxFromLocation = new JComboBox();
		label6 = new JLabel();
		cbxToLocation = new JComboBox();
		label7 = new JLabel();
		label8 = new JLabel();
		cbxFromDepartment = new JComboBox();
		label9 = new JLabel();
		cbxToDepartment = new JComboBox();
		label10 = new JLabel();
		label11 = new JLabel();
		cbxFromProject = new JComboBox();
		label12 = new JLabel();
		cbxToProject = new JComboBox();
		scrollPane1 = new JScrollPane();
		tblResult = new JTable();
		panel2 = new JPanel();
		btnExit = new JButton();

		//======== this ========
		setPreferredSize(new Dimension(500, 300));
		setOpaque(false);
		setLayout(new BorderLayout());

		//---- label1 ----
		label1.setText("Search Transfer");
		label1.setFont(new Font("Tahoma", Font.BOLD, 18));
		label1.setHorizontalAlignment(SwingConstants.CENTER);
		label1.setPreferredSize(new Dimension(145, 40));
		add(label1, BorderLayout.NORTH);

		//======== panel1 ========
		{
			panel1.setLayout(new GridBagLayout());
			((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {0, 0, 0, 0, 0, 0};
			((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
			((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0, 0.0, 1.0E-4};
			((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};

			//---- label13 ----
			label13.setText(" ");
			panel1.add(label13, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label2 ----
			label2.setText("EmpNumber:");
			panel1.add(label2, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(txtID, new GridBagConstraints(2, 0, 2, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label14 ----
			label14.setText(" ");
			panel1.add(label14, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 0), 0, 0));

			//---- label3 ----
			label3.setText("EmpName:");
			panel1.add(label3, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(txtName, new GridBagConstraints(2, 1, 2, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label4 ----
			label4.setText("Location");
			label4.setPreferredSize(new Dimension(50, 14));
			panel1.add(label4, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label5 ----
			label5.setText("FromLocation");
			panel1.add(label5, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxFromLocation, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label6 ----
			label6.setText("ToLocation:");
			panel1.add(label6, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxToLocation, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label7 ----
			label7.setText("Department");
			label7.setPreferredSize(new Dimension(50, 14));
			panel1.add(label7, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label8 ----
			label8.setText("FromDepartment:");
			panel1.add(label8, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxFromDepartment, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label9 ----
			label9.setText("ToDepartment:");
			panel1.add(label9, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxToDepartment, new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label10 ----
			label10.setText("Project");
			panel1.add(label10, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label11 ----
			label11.setText("FromProject:");
			panel1.add(label11, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxFromProject, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//---- label12 ----
			label12.setText("ToProject:");
			panel1.add(label12, new GridBagConstraints(2, 7, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));
			panel1.add(cbxToProject, new GridBagConstraints(3, 7, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 5, 10), 0, 0));

			//======== scrollPane1 ========
			{
				scrollPane1.setViewportView(tblResult);
			}
			panel1.add(scrollPane1, new GridBagConstraints(1, 8, 3, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 10), 0, 0));
		}
		add(panel1, BorderLayout.CENTER);

		//======== panel2 ========
		{
			panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));

			//---- btnExit ----
			btnExit.setText("Back");
			btnExit.setSelectedIcon(null);
			panel2.add(btnExit);
		}
		add(panel2, BorderLayout.SOUTH);
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	private JLabel label1;
	private JPanel panel1;
	private JLabel label13;
	private JLabel label2;
	private JTextField txtID;
	private JLabel label14;
	private JLabel label3;
	private JTextField txtName;
	private JLabel label4;
	private JLabel label5;
	private JComboBox cbxFromLocation;
	private JLabel label6;
	private JComboBox cbxToLocation;
	private JLabel label7;
	private JLabel label8;
	private JComboBox cbxFromDepartment;
	private JLabel label9;
	private JComboBox cbxToDepartment;
	private JLabel label10;
	private JLabel label11;
	private JComboBox cbxFromProject;
	private JLabel label12;
	private JComboBox cbxToProject;
	private JScrollPane scrollPane1;
	private JTable tblResult;
	private JPanel panel2;
	private JButton btnExit;
	// JFormDesigner - End of variables declaration //GEN-END:variables

	public void setModel(SearchModel model) {
		this.tblResult.setModel(model);
	}

	public String getID() {
		return this.txtID.getText().equals("") ? "%" : this.txtID.getText();
	}

	public String getName() {
		return this.txtName.getText().equals("") ? "%" : "%"
				+ this.txtName.getText() + "%";
	}

	public void setID(String string) {
		this.txtID.setText(string);
	}

	public void setName(String string) {
		this.txtName.setText(string);
	}

	public String getFromLocation() {
		return this.cbxFromLocation.getSelectedItem().toString().equals("") ? "%"
				: this.cbxFromLocation.getSelectedItem().toString();
	}

	public String getToLocation() {
		return this.cbxToLocation.getSelectedItem().toString().equals("") ? "%"
				: this.cbxToLocation.getSelectedItem().toString();
	}

	public String getFromProject() {
		return this.cbxFromProject.getSelectedItem().toString().equals("") ? "%"
				: this.cbxFromProject.getSelectedItem().toString();
	}

	public String getToProject() {
		return this.cbxToProject.getSelectedItem().toString().equals("") ? "%"
				: this.cbxToProject.getSelectedItem().toString();
	}

	public String getFromDepartment() {
		return this.cbxFromDepartment.getSelectedItem().toString().equals("") ? "%"
				: this.cbxFromDepartment.getSelectedItem().toString();
	}

	public String getToDepartment() {
		return this.cbxToDepartment.getSelectedItem().toString().equals("") ? "%"
				: this.cbxToDepartment.getSelectedItem().toString();
	}

	public void searchID(KeyListener listener) {
		// TODO Auto-generated method stub
		this.txtID.addKeyListener(listener);
	}

	public void searchName(KeyListener listener) {
		// TODO Auto-generated method stub
		this.txtName.addKeyListener(listener);

	}

	public void searchFromLocation(ActionListener listener) {
		// TODO Auto-generated method stub
		this.cbxFromLocation.addActionListener(listener);
	}

	public void searchToLocation(ActionListener listener) {
		this.cbxToLocation.addActionListener(listener);
	}

	public void searchFromProject(ActionListener listener) {
		this.cbxFromProject.addActionListener(listener);
	}

	public void searchToProject(ActionListener listener) {
		this.cbxToProject.addActionListener(listener);
	}

	public void searchFromDepartment(ActionListener listener) {
		this.cbxFromDepartment.addActionListener(listener);
	}

	public void searchToDepartment(ActionListener listener) {
		this.cbxToDepartment.addActionListener(listener);
	}

	public void loadLocation() throws SQLException, ClassNotFoundException,
			IOException {
		this.cbxFromLocation.addItem("");
		this.cbxToLocation.addItem("");
		for (String item : dao.loadComboBox("Location")) {
			this.cbxFromLocation.addItem(item);
			this.cbxToLocation.addItem(item);
		}
	}

	public void loadProject() throws SQLException, ClassNotFoundException,
			IOException {
		this.cbxFromProject.addItem("");
		this.cbxToProject.addItem("");
		for (String item : dao.loadComboBox("Project")) {
			this.cbxFromProject.addItem(item);
			this.cbxToProject.addItem(item);
		}
	}

	public void loadDepartment() throws SQLException, ClassNotFoundException,
			IOException {
		this.cbxFromDepartment.addItem("");
		this.cbxToDepartment.addItem("");
		for (String item : dao.loadComboBox("Department")) {
			this.cbxFromDepartment.addItem(item);
			this.cbxToDepartment.addItem(item);
		}
	}

	public void doExit(ActionListener listener) {
		this.btnExit.addActionListener(listener);
	}
}
